package cs301.msflags;

import cs301.game.GameAction;
import cs301.game.GameConfig;
import cs301.game.LocalGame;

public class MSFlagsGame extends LocalGame {
	// when a counter game is played, there are two players. Each player
	// is trying to get the counter value to either N or -N. This constant
	// is used to specify N.
	public static final int TARGET_MAGNITUDE = 5;

	// the counter value
	private int counter = 0;

	/**
	 * This ctor should be called when a new counter game is started
	 */
	public MSFlagsGame(GameConfig config, int whoseTurn) {
		super(config, whoseTurn);
		this.counter = 0; // counter starts at 0
	}

	/**
	 * since the counter game is a perfect information game, this method copies
	 * the data with no omissions.
	 */
	@Override
	public LocalGame getPlayerState(int playerIndex) {
		MSFlagsGame copy = new MSFlagsGame(getConfig(), this.whoseTurn);
		copy.counter = this.counter;
		return copy;
	}

	/**
	 * The only type of GameAction that should be sent is CounterMoveAction
	 */
	@Override
	public void applyAction(GameAction action) {
		MSFlagsMoveAction cma = (MSFlagsMoveAction)action;

		//Now it's the other player's turn
		this.whoseTurn = 1 - this.whoseTurn;
		
	}//applyAction

	@Override
	public boolean isGameOver() {
		return Math.abs(counter) >= TARGET_MAGNITUDE;
	}

	/**
	 * @return the id of the player whose target number has been reached or -1
	 *         if neither has.
	 */
	@Override
	public int getWinnerId() {
		if (counter >= TARGET_MAGNITUDE) {
			return 0;
		} else if (-counter >= TARGET_MAGNITUDE) {
			return 1;
		} else {
			return -1;
		}

	}// getWinnerId

}// class CounterGameState
